﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Idte.Common.Process
{
    public class ActionStep : Step
    {
        private Result _result;
        public ActionStep(ActionStepDescriptor descriptor) : base(descriptor) { }

        public Result Result
        {
            get { return _result ?? EmptyResult.Instance; }
            set { _result = value; }
        }

        public override IExecutionContext Execute(IExecutionContext context)
        {
            try
            {
                return InvokeAction(context);
            }
            catch
            {
                return new ErrorContext(context);
            }
        }


        protected IExecutionContext InvokeAction(IExecutionContext ctx)
        {
            ActionStepDescriptor desc = (ActionStepDescriptor)Descriptor;
            Result = desc.Action.Execute(ctx) as Result ?? EmptyResult.Instance;
            return ctx;
        }

        
    }
}
